<?php

function make_form($name,$questions=array(),$method="post",$action="",$args="") {
	echo '<form name="' . $name . '" id="' . $name . '" method="' . $method .'" ';
	if ($action != "") echo 'action="' . $action . '" ';
	echo $args . '>' . "\n";
	foreach ($questions as $question) {
		$question = array_pad($question,7,null);
		make_field($question[0],$question[1],$question[2],$question[3],$question[4],$question[5],$question[6]);
	}
	if (sizeof($questions)>0) echo '</form>' . "\n";
}
function make_field($name,$desc,$type,$value,$choices=array(),$size="",$args="") {
	echo "\t" . '<div class="form-row">' . "\n";
		if ($type !="hidden") {
			echo "\t\t" . '<div class="form-label">' . "\n";
			if (strlen($desc)>0)	echo "\t\t\t" . '<label for="' . $name . '">' . $desc . ": </label>\n";
			echo "\t\t" . '</div>' . "\n";
		}
		echo "\t\t" . '<div class="form-field">' . "\n";
		if ($type == "text") make_text_field($name,$value,$size,$args);
		else if ($type == "textarea")  make_textarea_field($name,$value,$size,$args);
		else if ($type == "select") make_select_field($name,$value,$choices,$args);
		else if ($type == "radio") make_radio_field($name,$value,$choices,$args);
		else if ($type == "checkbox") make_checkbox_field($name,$value,$choices,$args);	
		else if ($type == "hidden")  make_hidden_field($name,$value);
		else if ($type == "password") make_password_field($name,$value,$size,$args);
		else if ($type == "file") make_file_field($name,$value,$size,$args);
		else if ($type == "submit")make_submit_button($name,$value,$args);
		else if ($type == "reset")make_reset_button($name,$value,$args);
		echo "\t\t" . '</div>' . "\n";
	echo "\t" . "</div>\n";
}

function make_text_field($name,$value,$size,$args) {
	echo "\t\t\t" . '<input type="text" name="' . $name . '" id="' . $name . '" value="' . $value . '"';
	if ($size != "") echo 'size="' . $size .'"';
	echo ' ' . $args . " />\n";
}

function make_textarea_field($name,$value,$size,$args) {
	echo "\t\t\t" . '<textarea name="' . $name . '" id="' . $name . '"';
	if ($size != "") {
		$size = explode(",",$size);
		echo ' rows="' . $size[0] .'" ';
		echo 'cols="' . $size[1] .'" ';
	}
	echo ' ' . $args . '>' . $value . "</textarea>\n";
}

function make_select_field($name,$value,$choices,$args) {
	echo "\t\t\t" . '<select name="' . $name . '" id="' . $name . '" ' . $args . ">\n";
	foreach ($choices as $choice=>$desc) {
		echo "\t\t\t\t" . '<option value="' . $choice . '"';
		if ($choice == $value) echo ' selected="true"';
		echo '>' . $desc . "</option>\n";
	}
	echo "\t\t\t" . "</select>\n";
}

function make_radio_field($name,$value,$choices,$args) {
	foreach ($choices as $choice=>$desc) {
		echo "\t\t\t" . '<input type="radio" name="' . $name . '" id="' . $name . '[' . $choice . ']" ' . $args;
		if ($choice == $value) echo ' checked="true"';
		echo ' /><label for="' . $name . '[' . $choice . ']">' . $desc . "</label><br />\n";
	}
}

function make_checkbox_field($name,$value,$choices,$args) {
	foreach ($choices as $choice=>$desc) {
		echo "\t\t\t" . '<input type="checkbox" name="' . $name . '[' . $choice . ']" id="' . $name . '[' . $choice . ']" ' . $args;
		if ($choice == $value) echo ' checked="true"';
		echo ' /><label for="' . $name . '[' . $choice . ']">' . $desc . "</label><br />\n";
	}
}

function make_hidden_field($name,$value) {
	echo "\t\t\t" . '<input type="hidden" name="' . $name . '" id="' . $name . '" value="' . $value . '" />' . "\n";
}

function make_password_field($name,$value,$size,$args) {
	echo "\t\t\t" . '<input type="password" name="' . $name . '" id="' . $name . '" value="' . $value . '"';
	if ($size != "") echo 'size="' . $size .'"';	
	echo ' ' . $args . ' />' . "\n";
}

function make_file_field($name,$value,$size,$args) {
	echo "\t\t\t" . '<input type="file" name="' . $name . '" id="' . $name . '" value="' . $value . '"';
	if ($size != "") echo 'size="' . $size .'"';
	echo ' ' . $args . ' />' . "\n";
}

function make_submit_button($name,$value,$args) {
	echo "\t\t\t" . '<input type="submit" name="' . $name . '" id="' . $name . '" value="' . $value . '" ' . $args . " />\n";
}

function make_reset_button($name,$value,$args) {
	echo "\t\t\t" . '<input type="reset" name="' . $name . '" id="' . $name . '" value="' . $value . '" ' . $args . " />\n";
}

?>